package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xqyy.eval.model.DeptGroup;
import org.xqyy.eval.model.TDeptGroup;
import org.xqyy.eval.service.IDeptGroupService;
import org.xqyy.eval.service.IDeptService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-24.
 */

@Controller
@RequestMapping("/deptGroup")
public class DeptGroupController {

    @Autowired
    private IDeptGroupService deptGroupService;
    @Autowired
    private IDeptService deptService;

    @RequestMapping("/loadDeptGroups")
    @ResponseBody
    public Map<String, Object> loadDeptGroups(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap();
        String code = request.getParameter("code");
        if (StringUtils.hasLength(code)) {
            res.put("code", code);
        }
        List<Map<String, Object>> data = deptGroupService.load(res);
        res.put("root", data);
        return res;
    }

    @RequestMapping("/addDeptGroup")
    @ResponseBody
    public Map<String, Object> addDeptGroup(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String mark = request.getParameter("mark");
            res.put("code", code);
            res.put("name", name);
            res.put("mark", mark);
            deptGroupService.add(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/updateDeptGroup")
    @ResponseBody
    public Map<String, Object> updateDeptGroup(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String mark = request.getParameter("mark");
            res.put("code", code);
            if (StringUtils.hasLength(name)) {
                res.put("name", name);
            }
            if (StringUtils.hasLength(mark)) {
                res.put("res", mark);
            }
            deptGroupService.modify(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }


    @RequestMapping("/deleteDeptGroup")
    @ResponseBody
    public Map<String, Object> deleteDeptGroup(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            res.put("code", code);
            deptGroupService.delete(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/selectDeptAndGeptGroup")
    @ResponseBody
    public List<Map<String,Object>> selectDeptAndGeptGroup(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String,Object>> groupList = new ArrayList<Map<String,Object>>();
        List<DeptGroup> deptGroups =  deptGroupService.selecAllDepGroup();
        Map<String, Object> res = null;
        for (DeptGroup deptGroup : deptGroups) {
            res = new HashMap<>();
            res.put("groupname",deptGroup.getName());
            res.put("items",deptService.selectDeptByGroupId(deptGroup.getCode()));
            groupList.add(res);
        }
        return groupList;
    }

    @RequestMapping("/selectAllDeptGroup")
    @ResponseBody
    public List<TDeptGroup> selectAllDeptGroup() {
        return deptGroupService.selectAllTDeptGroup();
    }

    /**
     * 新的科室分类添加
     * @return
     */
    @RequestMapping("/addTDeptGroup")
    @ResponseBody
    public Boolean addTDeptGroup(TDeptGroup deptGroup) {
        try {
            if (deptGroupService.addTDeptGroup(deptGroup)){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 新的科室分类修改
     * @return
     */
    @RequestMapping("/updateTDeptGroup")
    @ResponseBody
    public Boolean updateTDeptGroup(TDeptGroup deptGroup) {
        try {
            if (deptGroupService.updateTDeptGroup(deptGroup)){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 新的科室分类查询
     * @return
     */
    @RequestMapping("/selectTDeptGroupById")
    @ResponseBody
    public TDeptGroup selectTDeptGroupById(TDeptGroup deptGroup) {
        return deptGroupService.findTDeptGroupById(deptGroup);
    }

    /**
     * 新的科室分类查询
     * @return
     */
    @RequestMapping("/delTDeptGroup")
    @ResponseBody
    public Boolean delTDeptGroup(TDeptGroup deptGroup) {
        try {
            if (deptGroupService.delTDeptGroup(deptGroup)){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

}
